package com.njust.controller;

import com.njust.domain.dto.ProductDTO;
import com.njust.domain.dto.ProductPageQueryDTO;
import com.njust.domain.vo.PageQueryVO;
import com.njust.result.Result;
import com.njust.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author Imak
 * @Date 2024/11/7 13:03
 * @产品controller
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping("/add")
    public Result<String> addProduct(@RequestBody ProductDTO productDTO){
        log.info("添加产品，{}",productDTO);
        int result = productService.add(productDTO);
        if(result >= 1) return Result.success("产品添加成功");
        else return Result.error("产品添加失败");
    }

    @PutMapping("/update")
    public Result<String> updateProduct(@RequestBody List<ProductDTO> productDTOList){
        log.info("修改产品，{}",productDTOList);
        int result = productService.update(productDTOList);
        if(result >= 1) return Result.success("产品修改成功");
        else return Result.error("产品修改失败");
    }

    @GetMapping("/pageQuery")
    public Result<PageQueryVO> pageQuery(ProductPageQueryDTO productPageQueryDTO){
        log.info("分页查询产品,参数为: {}",productPageQueryDTO);
        PageQueryVO PageQueryVO = productService.pageQuery(productPageQueryDTO);
        return Result.success(PageQueryVO);
    }
    @DeleteMapping("/delete")
    public Result<String> delete(Integer product_id){
        log.info("删除产品,id为: {}",product_id);
        int result = productService.delete(product_id);
        if(result >= 1) return Result.success("产品删除成功");
        else return Result.error("产品删除失败");
    }
}
